package fhq.hcmute.edu.vn.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.SocketException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import fhq.hcmute.edu.vn.config.AppConfig;
import fhq.hcmute.edu.vn.language.Language;
import fhq.hcmute.edu.vn.language.LanguageFactory;
import fhq.hcmute.edu.vn.network.NetworkCard;
import fhq.hcmute.edu.vn.network.NetworkUtil;

public class NetworkDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3800189673411544645L;
	public static final int OK = 1;
	public static final int CALCEL = 0;
	private final JPanel contentPanel = new JPanel();
	private JTable table;
	private final JButton btnPick;
	private final JButton btnClose;
	private ArrayList<NetworkCard> arrayList;
	private int dialogResult;
	private Language language;

	public final int getDialogResult() {
		return dialogResult;
	}

	public final void setDialogResult(final int dialogResult) {
		this.dialogResult = dialogResult;
	}

	/**
	 * Create the dialog.
	 */
	public NetworkDialog(final Frame parent, final boolean modal) {
		super(parent, modal);
		language = LanguageFactory.getInstance().getLanguage();
		btnPick = new JButton(language.btnPick());
		btnClose = new JButton(language.btnClose());
		setBounds(100, 100, 523, 204);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		setCenterScreen();
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 488, 97);
		contentPanel.add(scrollPane);

		try {
			arrayList = NetworkUtil.getListNetwork();
			configJTable(arrayList);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		scrollPane.setViewportView(table);

		btnPick.addActionListener(onClickOKButton);
		btnPick.setFont(new Font("Times New Roman", Font.PLAIN, 17));
		btnPick.setBounds(122, 119, 100, 30);

		contentPanel.add(btnPick);
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				dialogResult = CALCEL;
				dispose();
			}
		});
		btnClose.setFont(new Font("Times New Roman", Font.PLAIN, 17));
		btnClose.setBounds(275, 119, 100, 30);
		contentPanel.add(btnClose);
	}

	private void setCenterScreen() {
		int width = this.getWidth();
		int height = this.getHeight();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (screenSize.width / 2) - (width / 2);
		int y = (screenSize.height / 2) - (height / 2);
		this.setLocation(x, y);
	}

	private ActionListener onClickOKButton = new ActionListener() {
		public void actionPerformed(final ActionEvent arg0) {
			int selectedRow = table.getSelectedRow();
			if (selectedRow >= 0) {
				NetworkCard networkCard = arrayList.get(selectedRow);
				AppConfig.getInstance().setNetworkCard(networkCard);
				AppConfig.getInstance().commit();
				JOptionPane.showMessageDialog(NetworkDialog.this,
						language.netWorkDialogSaveNCSuccess(),
						language.netWorkDialogSaveNCSuccessTitle(),
						JOptionPane.INFORMATION_MESSAGE);
				dialogResult = OK;
				dispose();
			} else {
				JOptionPane.showMessageDialog(NetworkDialog.this,
						language.netWorkDialogDefaultCard(),
						language.netWorkDialogWarning(),
						JOptionPane.WARNING_MESSAGE);
			}
		}
	};

	private void configJTable(final ArrayList<NetworkCard> arrayList) {
		DefaultTableModel dtm = new DefaultTableModel() {
			private static final long serialVersionUID = -1492154389291323051L;

			@Override
			public boolean isCellEditable(final int row, final int column) {
				return false;
			}
		};
		dtm.addColumn(language.netWorkDialogTableID());
		dtm.addColumn(language.netWorkDialogTableNetworkCard());
		dtm.addColumn(language.netWorkDialogTableIpAddress());
		dtm.setRowCount(arrayList.size());
		for (int i = 0; i < arrayList.size(); i++) {
			NetworkCard networkCard = arrayList.get(i);
			dtm.setValueAt(i + 1, i, 0);
			dtm.setValueAt(networkCard.getDisplayName(), i, 1);
			dtm.setValueAt(networkCard.getIpAddress(), i, 2);
		}
		table = new JTable();
		table.setFont(new Font("Times New Roman", Font.PLAIN, 13));
		table.setModel(dtm);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
		rightRenderer.setHorizontalAlignment(JLabel.CENTER);
		table.getColumnModel().getColumn(0).setPreferredWidth(70);
		table.getColumnModel().getColumn(0).setCellRenderer(rightRenderer);
		table.getColumnModel().getColumn(1).setPreferredWidth(315);
		// table.getColumnModel().getColumn(1).setCellRenderer(rightRenderer);
		table.getColumnModel().getColumn(2).setPreferredWidth(100);
		table.getColumnModel().getColumn(2).setCellRenderer(rightRenderer);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	}
}
